package com.beaven.imclient.fragment;

import android.app.ProgressDialog;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.EditText;
import com.beaven.imclient.R;
import com.beaven.imclient.base.BaseFragment;
import com.beaven.imclient.presenter.account.UpdatePwdContract;
import com.beaven.imclient.presenter.account.UpdaterPwdPresenter;
import com.beaven.imclient.utils.ToastUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class PwdFragment extends BaseFragment implements UpdatePwdContract.View {

  public static PwdFragment newInstance() {
    return new PwdFragment();
  }

  private UpdatePwdContract.Presenter presenter;
  private ProgressDialog dialog;
  private EditText edit_pwd_old;
  private EditText edit_pwd_new;
  private EditText edit_pwd_confirm;

  @Override protected int getLayoutId() {
    return R.layout.fragment_pwd;
  }

  @Override protected void initView(View rootView) {
    super.initView(rootView);
    edit_pwd_old = (EditText) rootView.findViewById(R.id.edit_pwd_old);
    edit_pwd_new = (EditText) rootView.findViewById(R.id.edit_pwd_new);
    edit_pwd_confirm = (EditText) rootView.findViewById(R.id.edit_pwd_confirm);
    rootView.findViewById(R.id.bt_save).setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        presenter.updatePwd();
      }
    });
  }

  @Override protected void initData() {
    super.initData();
    setPresenter(new UpdaterPwdPresenter(this));
  }

  @Override public void setPresenter(UpdatePwdContract.Presenter presenter) {
    this.presenter = presenter;
  }

  @Override public void onResume() {
    super.onResume();
    presenter.subscribe();
  }

  @Override public void onPause() {
    super.onPause();
    presenter.unSubscribe();
  }

  @Override public String getOldPwd() {
    return edit_pwd_old.getText().toString().trim();
  }

  @Override public String getNewPwd() {
    return edit_pwd_new.getText().toString().trim();
  }

  @Override public String getNewConfirmPwd() {
    return edit_pwd_confirm.getText().toString().trim();
  }

  @Override public void showLoading() {
    dialog = ProgressDialog.show(getActivity(), "", getString(R.string.load_now));
  }

  @Override public void hideLoading() {
    if (dialog != null) {
      dialog.dismiss();
    }
  }

  @Override public void showText(int stringId) {
    ToastUtil.show(stringId);
  }

  @Override public void finish() {
    getActivity().finish();
  }
}
